package com.siam.package_rider.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.siam.package_common.constant.Quantity;
import com.siam.package_common.entity.BasicResult;
import com.siam.package_common.exception.StoneCustomerException;
import com.siam.package_merchant.entity.MerchantWithdrawRecord;
import com.siam.package_rider.entity.TbRider;
import com.siam.package_rider.entity.TbRiderWithdrawRecord;
import com.siam.package_rider.service.TbRiderService;
import com.siam.package_rider.service.TbRiderWithdrawRecordService;
import com.siam.package_user.entity.MemberTradeRecord;
import com.siam.package_user.feign.MemberTradeRecordFeignApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * 提供给其他服务Feign客户端调用的用户模块相关接口
 */
@RestController
public class RiderWithdrawRecordFeignProvider implements RiderWithdrawRecordFeignApi {

    @Autowired
    private TbRiderWithdrawRecordService riderWithdrawRecordService;

    @Autowired
    private MemberTradeRecordFeignApi memberTradeRecordFeignApi;

    @Autowired
    private TbRiderService riderService;

    @Override
    public BasicResult<List<TbRiderWithdrawRecord>> queryPaymentProcessingRecord(Long riderId) {
        LambdaQueryWrapper<TbRiderWithdrawRecord> merchantQueryWrapper = new LambdaQueryWrapper<>();
        merchantQueryWrapper.eq(TbRiderWithdrawRecord::getRiderId, riderId);
        merchantQueryWrapper.eq(TbRiderWithdrawRecord::getAuditStatus, MerchantWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        List<TbRiderWithdrawRecord> withdrawRecordList = riderWithdrawRecordService.list(merchantQueryWrapper);
        return BasicResult.success(withdrawRecordList);
    }

    @Override
    public BasicResult updateAuditStatusSuccess(String orderNo) {
        // 查询符合条件的记录
        LambdaQueryWrapper<TbRiderWithdrawRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbRiderWithdrawRecord::getOutTradeNo, orderNo);
        queryWrapper.eq(TbRiderWithdrawRecord::getAuditStatus, TbRiderWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        TbRiderWithdrawRecord dbTbRiderWithdrawRecord = riderWithdrawRecordService.getOne(queryWrapper);
        if(dbTbRiderWithdrawRecord == null){
            throw new StoneCustomerException("[修改骑手提现记录状态]该记录查询为空,orderNo=" + orderNo);
        }

        // 修改审核状态
        TbRiderWithdrawRecord updateTbRiderWithdrawRecord = new TbRiderWithdrawRecord();
        updateTbRiderWithdrawRecord.setId(dbTbRiderWithdrawRecord.getId());
        updateTbRiderWithdrawRecord.setAuditStatus(TbRiderWithdrawRecord.AUDIT_STATUS_SUCCESS);
        updateTbRiderWithdrawRecord.setAuditTime(new Date());
        updateTbRiderWithdrawRecord.setUpdateTime(new Date());
        riderWithdrawRecordService.updateById(updateTbRiderWithdrawRecord);

        //添加用户交易记录
        MemberTradeRecord insertMemberTradeRecord = new MemberTradeRecord();
        insertMemberTradeRecord.setRiderId(dbTbRiderWithdrawRecord.getRiderId());
        insertMemberTradeRecord.setOutTradeNo(orderNo);
        insertMemberTradeRecord.setType(Quantity.INT_7);
        insertMemberTradeRecord.setPaymentMode(Quantity.INT_1);
        insertMemberTradeRecord.setAmount(dbTbRiderWithdrawRecord.getWithdrawAmount());
        insertMemberTradeRecord.setStatus(Quantity.INT_2);
        insertMemberTradeRecord.setCreateTime(new Date());
        insertMemberTradeRecord.setUpdateTime(new Date());
        memberTradeRecordFeignApi.insertSelective(insertMemberTradeRecord);

        //TODO-发消息通知用户
        return BasicResult.success();
    }

    @Override
    public BasicResult updateAuditStatusFail(String orderNo, String auditReason) {
        // 查询符合条件的记录
        LambdaQueryWrapper<TbRiderWithdrawRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbRiderWithdrawRecord::getOutTradeNo, orderNo);
        queryWrapper.eq(TbRiderWithdrawRecord::getAuditStatus, TbRiderWithdrawRecord.AUDIT_STATUS_PAYMENT_PROCESSING);
        TbRiderWithdrawRecord dbTbRiderWithdrawRecord = riderWithdrawRecordService.getOne(queryWrapper);
        if(dbTbRiderWithdrawRecord == null){
            throw new StoneCustomerException("[修改骑手提现记录状态]该记录查询为空,orderNo=" + orderNo);
        }

        // 修改审核状态
        TbRiderWithdrawRecord updateTbRiderWithdrawRecord = new TbRiderWithdrawRecord();
        updateTbRiderWithdrawRecord.setId(dbTbRiderWithdrawRecord.getId());
        updateTbRiderWithdrawRecord.setAuditStatus(TbRiderWithdrawRecord.AUDIT_STATUS_FAIL);
        updateTbRiderWithdrawRecord.setAuditReason(auditReason);
        updateTbRiderWithdrawRecord.setAuditTime(new Date());
        updateTbRiderWithdrawRecord.setUpdateTime(new Date());
        riderWithdrawRecordService.updateById(updateTbRiderWithdrawRecord);

        //添加用户交易记录
        MemberTradeRecord insertMemberTradeRecord = new MemberTradeRecord();
        insertMemberTradeRecord.setRiderId(dbTbRiderWithdrawRecord.getRiderId());
        insertMemberTradeRecord.setOutTradeNo(orderNo);
        insertMemberTradeRecord.setType(Quantity.INT_7);
        insertMemberTradeRecord.setPaymentMode(Quantity.INT_1);
        insertMemberTradeRecord.setAmount(dbTbRiderWithdrawRecord.getWithdrawAmount());
        insertMemberTradeRecord.setStatus(Quantity.INT_3);
        insertMemberTradeRecord.setCreateTime(new Date());
        insertMemberTradeRecord.setUpdateTime(new Date());
        memberTradeRecordFeignApi.insertSelective(insertMemberTradeRecord);

        TbRider dbRider = riderService.getById(dbTbRiderWithdrawRecord.getRiderId());

        // 提现失败后置处理
        riderWithdrawRecordService.handleWithdrawFail(dbTbRiderWithdrawRecord, dbRider);

        return BasicResult.success();
    }
}